import router, {adminRoutes} from './router';
import store from './store';
import { Message } from 'element-ui';
import NProgress from 'nprogress' ;
import 'nprogress/nprogress.css' ;
import { getToken } from '@/utils/auth' ;
import defaultSettings from '@/settings';

NProgress.configure({ showSpinner: false });

const whiteList = ['/login', '/register'] // no redirect whitelist


router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = defaultSettings.title

  // determine whether the user has logged in
  const hasToken = getToken()
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else if(to.path === '/register') {
      next({ path: '/' })
      NProgress.done()
    } else {
      const hasRoles = store.getters.roles && store.getters.roles.length > 0
      if (hasRoles) {
        next()
      } else {
        try {
          const roles = await store.dispatch('user/getInfo');
          const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
          router.addRoutes(accessRoutes)
          console.log()
          // 默认路径
          if (roles.includes('管理员')) {
            next({ path: '/user' })
          } else if (roles.includes('居民')) {
            next({ path: '/user' })
          }
          //next({ ...to, replace: true })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
